package com.ruoyi.system.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.AddSuperviseNoticeDto;
import com.ruoyi.system.domain.SysProblemNotice;
import com.ruoyi.system.domain.updateSuperviseNoticeDto;
import com.ruoyi.system.mapper.SysProblemNoticeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysSuperviseHistoryMapper;
import com.ruoyi.system.domain.SysSuperviseHistory;
import com.ruoyi.system.service.ISysSuperviseHistoryService;

/**
 * 督查历史记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-31
 */
@Service
public class SysSuperviseHistoryServiceImpl implements ISysSuperviseHistoryService {
    @Autowired
    private SysSuperviseHistoryMapper sysSuperviseHistoryMapper;

    @Autowired
    private SysProblemNoticeMapper sysProblemNoticeMapper;

    /**
     * 查询督查历史记录
     *
     * @param historyId 督查历史记录主键
     * @return 督查历史记录
     */
    @Override
    public SysSuperviseHistory selectSysSuperviseHistoryByHistoryId(Long historyId) {

        SysSuperviseHistory superviseHistory = sysSuperviseHistoryMapper.selectSysSuperviseHistoryByHistoryId(historyId);


        SysProblemNotice problemNotice = new SysProblemNotice();
        problemNotice.setHistoryId(historyId);
        List<SysProblemNotice> problemNoticeList = sysProblemNoticeMapper.selectSysProblemNoticeList(problemNotice);

        if (CollUtil.isNotEmpty(problemNoticeList)){
            superviseHistory.setProblemNoticeList(problemNoticeList);
        }


        return superviseHistory;
    }

    /**
     * 查询督查历史记录列表
     *
     * @param sysSuperviseHistory 督查历史记录
     * @return 督查历史记录
     */
    @Override
    public List<SysSuperviseHistory> selectSysSuperviseHistoryList(SysSuperviseHistory sysSuperviseHistory) {
        return sysSuperviseHistoryMapper.selectSysSuperviseHistoryList(sysSuperviseHistory);
    }

    /**
     * 新增督查历史记录
     *
     * @param sysSuperviseHistory 督查历史记录
     * @return 结果
     */
    @Override
    public int insertSysSuperviseHistory(SysSuperviseHistory sysSuperviseHistory) {
        sysSuperviseHistory.setCreateTime(DateUtils.getNowDate());
        return sysSuperviseHistoryMapper.insertSysSuperviseHistory(sysSuperviseHistory);
    }

    /**
     * 修改督查历史记录
     *
     * @param sysSuperviseHistory 督查历史记录
     * @return 结果
     */
    @Override
    public int updateSysSuperviseHistory(SysSuperviseHistory sysSuperviseHistory) {
        sysSuperviseHistory.setUpdateTime(DateUtils.getNowDate());
        return sysSuperviseHistoryMapper.updateSysSuperviseHistory(sysSuperviseHistory);
    }

    /**
     * 批量删除督查历史记录
     *
     * @param historyIds 需要删除的督查历史记录主键
     * @return 结果
     */
    @Override
    public int deleteSysSuperviseHistoryByHistoryIds(Long[] historyIds) {
        return sysSuperviseHistoryMapper.deleteSysSuperviseHistoryByHistoryIds(historyIds);
    }

    /**
     * 删除督查历史记录信息
     *
     * @param historyId 督查历史记录主键
     * @return 结果
     */
    @Override
    public int deleteSysSuperviseHistoryByHistoryId(Long historyId) {
        return sysSuperviseHistoryMapper.deleteSysSuperviseHistoryByHistoryId(historyId);
    }


    @Override
    public AjaxResult addProblemNotice(AddSuperviseNoticeDto dto) {


        SysSuperviseHistory sysSuperviseHistory = new SysSuperviseHistory();
        BeanUtils.copyProperties(dto.getSuperviseHistory(), sysSuperviseHistory);
        sysSuperviseHistoryMapper.insertSysSuperviseHistory(sysSuperviseHistory);


        dto.getProblemNoticeList().forEach(obj -> {

            SysProblemNotice problemNotice = new SysProblemNotice();
            BeanUtils.copyProperties(obj, problemNotice);
            problemNotice.setHistoryId(sysSuperviseHistory.getHistoryId());

            sysProblemNoticeMapper.insertSysProblemNotice(problemNotice);
        });


        return AjaxResult.success();
    }


    @Override
    public AjaxResult updateProblemNote(updateSuperviseNoticeDto dto) {

        for (SysProblemNotice problemNotice : dto.getProblemNoticeList()) {

            SysProblemNotice sysProblemNotice = new SysProblemNotice();
            sysProblemNotice.setNoticeId(problemNotice.getNoticeId());
            sysProblemNotice.setRectificationPhotoUrl(problemNotice.getRectificationPhotoUrl());
            sysProblemNotice.setRectificationResult(problemNotice.getRectificationResult());

            sysProblemNoticeMapper.updateSysProblemNotice(sysProblemNotice);
        }

        return AjaxResult.success();

    }
}
